{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4d4a44dc",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "1. 函数的定义 ： def\n",
    "2. 函数名的定义规则-->变量名一样的规则\n",
    "3. 调用函数的方式\n",
    "4. 参数：\n",
    ">形式参数（定义函数）\n",
    ">实际参数（调用函数）\n",
    ">有多个参数时：\n",
    ">>1. 关键字参数\n",
    ">>2. 位置参数（调用函数时）---> 关键字参数要均放在末尾（多个不考虑顺序）\n",
    ">>3. 默认参数（定义函数时）---> 默认参数均放在末尾（多个不考虑顺序）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bdfcbf9",
   "metadata": {},
   "source": [
    "# 本周知识\n",
    "> 1. 函数的返回值\n",
    "> 2. 函数的参数类型\n",
    "> 3. 模块与函数的关系\n",
    "> 4. 定义一个模块，然后调用模块\n",
    ">> 1. 调用模块\n",
    ">> 2. 调用模块中的函数\n",
    "> 5. 模块与函数实际"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14f68819",
   "metadata": {},
   "source": [
    "# 函数的返回值\n",
    "> 1. 函数一定有返回值（无论是什么编程语言）\n",
    "> 2. 在其他编程语言中，返回值一定要写，不写会报错，对于py（更高级的语言），\n",
    "返回值如果不写，给定默认值None\n",
    "> 3. 返回值一般为函数体运行的结果(实践1），也可以是自定义的函数返回值（实践2）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a313b1da",
   "metadata": {},
   "outputs": [],
   "source": [
    "#实践1\n",
    "def get_formatted_name(first_name,last_name):\n",
    "    \"\"\"返回整洁的姓名\"\"\"\n",
    "    full_name = first_name + ' '+ last_name\n",
    "    # 函数的返回结果为 full_name\n",
    "    return full_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "cff017bb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Li Hua\n"
     ]
    }
   ],
   "source": [
    "print(get_formatted_name(\"Li\",\"Hua\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3f03fc1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "#实践2\n",
    "def log_in (username,password):\n",
    "    \"\"\"验证登陆信息\"\"\"\n",
    "    if username == \"lihua\" and password == 123456:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d1b355b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log_in(\"lihua\",12345)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97e4a455",
   "metadata": {},
   "source": [
    "# 函数的参数\n",
    "1. 函数输入的参数\n",
    "> 1. 函数可以输入复杂的参数：\n",
    ">> 1. 函数参数为列表\n",
    ">> 2. 函数参数为字典\n",
    "> 2. 函数可以输入不确定个数的参数\n",
    ">> 1. *args\n",
    ">> 2. ** kwargs\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "968a9b92",
   "metadata": {},
   "outputs": [],
   "source": [
    "def list_test(names):\n",
    "    \"\"\"测试输入参数为列表\"\"\"\n",
    "    print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2c70b5e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['lihua', 'xiaofeng', 'ziqi']\n"
     ]
    }
   ],
   "source": [
    " list_test([\"lihua\",\"xiaofeng\",\"ziqi\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0c3f58f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def dict_text(request_header):\n",
    "    \"\"\"测试输入参数为字典\"\"\"\n",
    "    print(request_header)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d27976dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# http://httpbin.org/get\n",
    "httpbin_get = {\n",
    "  \"args\": {}, \n",
    "  \"headers\": {\n",
    "    \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\", \n",
    "    \"Accept-Encoding\": \"gzip, deflate\", \n",
    "    \"Accept-Language\": \"zh-CN,zh;q=0.9\", \n",
    "    \"Host\": \"httpbin.org\", \n",
    "    \"Upgrade-Insecure-Requests\": \"1\", \n",
    "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36\", \n",
    "    \"X-Amzn-Trace-Id\": \"Root=1-61ad737f-7f5950b167a70a144d4c082b\"\n",
    "  }, \n",
    "  \"origin\": \"210.21.79.236\", \n",
    "  \"url\": \"http://httpbin.org/get\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4b98e7d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Host': 'httpbin.org', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36', 'X-Amzn-Trace-Id': 'Root=1-61ad737f-7f5950b167a70a144d4c082b'}\n"
     ]
    }
   ],
   "source": [
    "dict_text(httpbin_get[\"headers\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e30af78b",
   "metadata": {},
   "source": [
    "2. 函数可以输入不确定个数的参数\n",
    ">> 1. *args\n",
    ">>> 1.args可以接受多个数据（单值）\n",
    "args 将多个数据存在一个元组当中，实际还是一个变量\n",
    ">> 2. ** kwargs\n",
    ">>> 1. kwargs 可以接受多个数据(键值对)==> 关键字参数\n",
    "kwargs 将多个 键值对 的数据存放在一个 字典 中，实际上还是一个变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "a6b830cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "#*args\n",
    "def food_ingred(food,*ingreds):\n",
    "    \"\"\"食物配料单\"\"\"\n",
    "    return food,ingreds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "87c98a21",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('pizza', ('mushroom', 'green peppers', 'extra cheese'))"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "food_ingred(\"pizza\",\"mushroom\",\"green peppers\",\"extra cheese\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "87981abe",
   "metadata": {},
   "outputs": [],
   "source": [
    "# **kwargs\n",
    "def person(stud_name,**stud_info):\n",
    "    \"\"\"学生个人信息完善表\"\"\"\n",
    "    return stud_name,stud_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "deca4464",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('lihua', {'age': 18, 'job': 'stuadent', 'address': 'guangzhou'})"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person(\"lihua\",age=18,job=\"stuadent\",address=\"guangzhou\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "5ad4a260",
   "metadata": {},
   "outputs": [],
   "source": [
    "def func_name(arg1,arg2,*arg,**kwargs):\n",
    "    \"\"\"考虑比较周到的函数参数\"\"\"\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be6fac31",
   "metadata": {},
   "source": [
    "# 2.输出的返回值\n",
    "> 1. 返回值可以是 字符串，数值，列表，字典等数据（单一值）\n",
    "> 2. 返回值是唯一的，不可以有两个（其他语言）\n",
    "> 3. py可以输出多个返回值，结果可以用一个元组进行封装并返回（本质不违背函数定义）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "6536e836",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test1_return():\n",
    "    \"\"\"测试返回值\"\"\"\n",
    "    return \"你好\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "b90b9eb1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test1_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "086c82a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test2_return():\n",
    "    \"\"\"测试返回值\"\"\"\n",
    "    return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "362910d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test2_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ffff22a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test3_return():\n",
    "    \"\"\"测试返回值\"\"\"\n",
    "    return [1,2,3,4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1c20bc19",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test3_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "8bfb2850",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test4_return():\n",
    "    \"\"\"测试返回值\"\"\"\n",
    "    return httpbin_get[\"headers\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "c29becdd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',\n",
       " 'Accept-Encoding': 'gzip, deflate',\n",
       " 'Accept-Language': 'zh-CN,zh;q=0.9',\n",
       " 'Host': 'httpbin.org',\n",
       " 'Upgrade-Insecure-Requests': '1',\n",
       " 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36',\n",
       " 'X-Amzn-Trace-Id': 'Root=1-61ad737f-7f5950b167a70a144d4c082b'}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test4_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "05c582b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test5_return():\n",
    "    \"\"\"测试返回值\"\"\"\n",
    "    return 1,2 ,\"lihua\",[1,2,3],{\"name\":\"alex\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "20a8c502",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, 'lihua', [1, 2, 3], {'name': 'alex'})"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test5_return()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3aa1a496",
   "metadata": {},
   "source": [
    "# 3. 模块\n",
    "> 1. 使用模块可以隐藏函数代码\n",
    "> 2. 使用的人不需要会底层代码（只需要会使用模块jike）\n",
    "\n",
    ">3. 调用模块的方法\n",
    ">>1. 必须先调用模块，才可以使用\n",
    ">>2. import moudle_name 直接调用整个模块，此时模块的所有函数处于备用状态，使用的方法为：moudle_name.func_name()\n",
    ">>3. from moudle_name import func_name,此时，仅调用模块的某一个函数，只能使用这个模块的该函数，使用方法为 func_name()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "a923ea2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#调用模块方法1 ，调用整个模块\n",
    "import study"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "86bbabe5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o'}"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "study.search4vowels(\"hello zhichao\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "21e92e29",
   "metadata": {},
   "outputs": [],
   "source": [
    "#调用模块中方法2，调用模块中某个函数\n",
    "from study import search4vowels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "0e2f69e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o'}"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4vowels(\"hello zhichao\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c85372ee",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
